﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace BLL
{
	public class QLTBLoiMayTinh
	{
		/// <summary>
		/// Lấy tất cả lỗi máy tính
		/// </summary>
		public static IEnumerable<ThongBaoLoiMayTinh> GetAll()
		{
			return Generic.GetAll<ThongBaoLoiMayTinh>();
		}

		/// <summary>
		/// Thêm lỗi máy tính
		/// </summary>
		public static ActionResult Insert(ThongBaoLoiMayTinh lmt)
		{
			ActionResult ac=new ActionResult("Thêm lỗi máy tính");
			try
			{
 				if(!Generic.Insert<ThongBaoLoiMayTinh>(lmt))
				{
					ac.Exceptions.Add(new Exception("Lỗi"));
				}
			}
			catch(Exception ex)
			{
				ac.Exceptions.Add(ex);
			}
			return ac;
		}

		/// <summary>
		/// Xóa lỗi máy tính
		/// </summary>
		public static int Delete(ThongBaoLoiMayTinh lmt)
		{
			return Generic.DeleteByCondition<ThongBaoLoiMayTinh>(lmt1 => lmt1.MaLoiMT == lmt.MaLoiMT);
		}

		/// <summary>
		/// Xóa lỗi máy tính thuộc tính
		/// </summary>
		public static bool DeleteByID(int malmt)
		{
			return Generic.DeleteByCondition<ThongBaoLoiMayTinh>(x => x.MaLoiMT == malmt) > 0;
		}

		/// <summary>
		/// Xóa tất cả lỗi máy tính
		/// </summary>
		public static int DeleteAll(IEnumerable<ThongBaoLoiMayTinh> ps)
		{
			return Generic.DeleteByCondition<ThongBaoLoiMayTinh>(lmt => ps.Where(x => x.MaLoiMT == lmt.MaLoiMT).Count() == 1);
		}

		/// <summary>
		/// Cập nhật lỗi máy tính
		/// </summary>
		public static int Update(ThongBaoLoiMayTinh lmt)
		{
			return Generic.EditByCondition<ThongBaoLoiMayTinh>(k => k.MaLoiMT == lmt.MaLoiMT, k =>
			{
				k.MaLoiMT = lmt.MaLoiMT;
				k.TenPhanBiLoi = lmt.TenPhanBiLoi;
				k.ChiTietLoi = lmt.ChiTietLoi;
				
			});
		}

		/// <summary>
		/// Tìm lỗi máy tính theo điều kiện
		/// </summary>
		public static IEnumerable<ThongBaoLoiMayTinh> FindByCondition(Func<ThongBaoLoiMayTinh, bool> func)
		{
			return Generic.FindByCondition<ThongBaoLoiMayTinh>(func);
		}

		/// <summary>
		/// Tìm lỗi máy tính theo thuộc tính
		/// </summary>
		public static ThongBaoLoiMayTinh FindByID(int maloi)
		{
			return Generic.FindByCondition<ThongBaoLoiMayTinh>(x => x.MaLoiMT == maloi).ToList()[0];
		}

        #region Tìm Kiếm
        /// <summary>
        /// Tim Kiếm Theo Dieu Kien
        /// </summary>
        public enum DKTimKiem
        {
            MaLMT = 0,
            TenPBL,
            ChiTietLoi,
            MaLTB
        };
        public static void TimKiem(DataGridView dtgvQLTB, DKTimKiem dk, string dktimkiem)
        {
            try
            {
                switch (dk)
                {
                    case DKTimKiem.MaLMT:
                        dtgvQLTB.DataSource = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.MaLoiMT.ToString(), dktimkiem));
                        break;
                    case DKTimKiem.TenPBL:
                        dtgvQLTB.DataSource = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.TenPhanBiLoi.ToLower(), dktimkiem));
                        break;
                    case DKTimKiem.ChiTietLoi:
                        dtgvQLTB.DataSource = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.ChiTietLoi.ToLower(), dktimkiem));
                        break;
                    case DKTimKiem.MaLTB:
                        dtgvQLTB.DataSource = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.MaLoiTB.ToString(), dktimkiem));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Xắp Xếp
        static int click = 0;

        public static void XapXep(DataGridView dgvDSLoiMT, DataGridViewCellEventArgs e)
        {
            IEnumerable<ThongBaoLoiMayTinh> tb = null;
            if (dgvDSLoiMT.Columns[e.ColumnIndex].Name == "MaLoiMT")
            {
                click++;

                if (click == 1)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.MaLoiMT
                         select thietbi;
                }
                else if (click == 2)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.MaLoiMT descending
                         select thietbi;
                    click = 0;
                }

                dgvDSLoiMT.DataSource = tb.ToList();
            }
            else if (dgvDSLoiMT.Columns[e.ColumnIndex].Name == "TenPhanBL")
            {
                click++;
                if (click == 1)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.TenPhanBiLoi
                         select thietbi;
                }
                else if (click == 2)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.TenPhanBiLoi descending
                         select thietbi;
                    click = 0;
                }

                dgvDSLoiMT.DataSource = tb.ToList();
            }
            else if (dgvDSLoiMT.Columns[e.ColumnIndex].Name == "ChiTietLoi")
            {
                click++;
                if (click == 1)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.ChiTietLoi
                         select thietbi;
                }
                else if (click == 2)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.ChiTietLoi descending
                         select thietbi;
                    click = 0;
                }

                dgvDSLoiMT.DataSource = tb.ToList();
            }
            else if (dgvDSLoiMT.Columns[e.ColumnIndex].Name == "MaLTB")
            {
                click++;
                if (click == 1)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.MaLoiTB
                         select thietbi;
                }
                else if (click == 2)
                {
                    tb = from thietbi in GetAll().ToList()
                         orderby thietbi.MaLoiTB descending
                         select thietbi;
                    click = 0;
                }

                dgvDSLoiMT.DataSource = tb.ToList();
            }
        }
        #endregion
	}
}
